﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using ITOMMS.Entity;
using Watson.Core.Tools;
using ITOMMS.Business;

namespace ITOMMS.Web.UserManage
{
    public partial class SoftWareList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataShow(GetSearchSql());


                if (SessionHelper.UserRoleGroup != PubEnums.RoleGroup.管理员.ToString())
                {
                    DataControlField colEdit = (DataControlField)this.dgdaoim_sus_plyedr.Columns[5];
                    colEdit.Visible = false;

                    DataControlField colDelete = (DataControlField)this.dgdaoim_sus_plyedr.Columns[6];
                    colDelete.Visible = false;
                }
            }
        }

        private void DataShow(string sql)
        {
            tb软件清单Entity entity = new tb软件清单Entity();
            AspNetPager1.RecordCount = entity.GetTotalRecords();
            this.dgdaoim_sus_plyedr.DataSource = entity.GetAllByQuery(sql, "order by id desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            dgdaoim_sus_plyedr.DataBind();

        }


        protected void CMGridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //try
            //{
            //    if (e.CommandName.ToString() == "del")
            //    {
            //        if (e.CommandArgument.ToString() != "")
            //        {

            //            if (new tbCustEmpSoftListEntity().Delete(int.Parse(e.CommandArgument.ToString())) > 0)
            //            {
            //                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.succeedInfo('删除成功。',300,180,'提示',null);</script>");
            //                DataShow(GetSearchSql());
            //            }
            //            else
            //            {
            //                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('删除失败。',300,180,'提示',null);</script>");
            //            }

            //        }
            //    }
            //}
            //catch (APPException appex)
            //{
            //    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('" + appex.Message + "。',300,180,'提示',null);</script>");
            //}
            //catch (Exception ex)
            //{
            //    log4netHelper.error("", ex);
            //    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('删除失败。',300,180,'提示',null);</script>");
            //}
        }

        protected void CMGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowIndex > -1)
            //{
            //    //如果是绑定数据行 
            //    if (e.Row.RowType == DataControlRowType.DataRow)
            //    {
            //        if (e.Row.Cells[1].Text.Length > 20)
            //        {
            //            e.Row.Cells[1].Text = e.Row.Cells[1].Text.Substring(0, 20);
            //        }


            //        if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            //        {
            //            ((LinkButton)e.Row.Cells[5].FindControl("lbtnDelete")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
            //        }
            //    }
            //}
        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            DataShow(GetSearchSql());
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            DataShow(GetSearchSql());
        }


        private string GetSearchSql()
        {

            StringBuilder sb = new StringBuilder();
            sb.Append(" 1=1 ");
           
            if (txtSoftName.Text.Trim().Length > 0)
            {
                sb.Append(" and  软件名称 like '%").Append(txtSoftName.Text.Trim()).Append("%'");
            }
            if (txtSoftType.Text.Trim().Length > 0)
            {
                sb.Append(" and  软件类别 like '%").Append(txtSoftType.Text.Trim()).Append("%'");
            }
            return sb.ToString();
        }

        protected void dgdaoim_sus_plyedr_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.ToString() == "del")
                {
                    if (e.CommandArgument.ToString() != "")
                    {

                        if (new tb软件清单Entity().Delete(int.Parse(e.CommandArgument.ToString())) > 0)
                        {
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.succeedInfo('删除成功。',300,180,'提示',null);</script>");
                            DataShow(GetSearchSql());
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('删除失败。',300,180,'提示',null);</script>");
                        }

                    }
                }
            }
            catch (APPException appex)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('" + appex.Message + "。',300,180,'提示',null);</script>");
            }
            catch (Exception ex)
            {
                log4netHelper.error("", ex);
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.errorInfo('删除失败。',300,180,'提示',null);</script>");
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("SoftWareAdd.aspx", true);
        }


    }
}
